package com.marlowe.asyouknow.service;

import com.alibaba.fastjson.JSON;
import com.marlowe.asyouknow.pojo.Content;
import com.marlowe.asyouknow.pojo.News;
import com.marlowe.asyouknow.pojo.Pictures;
import com.marlowe.asyouknow.utils.HtmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @program: as-you-know
 * @description: 爬虫服务类
 * @author: Marlowe
 * @create: 2020-12-21 11:25
 **/
@Service
public class ParseService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 解析爬取的京东数据放入es索引中
     *
     * @param keywords
     * @return
     * @throws IOException
     */
    public Boolean parseJD(String keywords) throws IOException {
        List<Content> contents = new HtmlParseUtil().parseJD(keywords);
        // 把查询到的数据放入es中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");

        for (int i = 0; i < contents.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("jd_goods")
                            .source(JSON.toJSONString(contents.get(i)), XContentType.JSON));
        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("爬取数据成功，插入es成功！");
        return !bulk.hasFailures();
    }


    /**
     * 爬取南方周末新闻网站
     *
     * @return
     */
    public Boolean parseNews() throws Exception {
        // 使用网页解析工具类爬取网页
        List<News> news = new HtmlParseUtil().parseNews();
        // 如果没有爬取到网页
        if (news.size() == 0) {
            System.out.println("未爬取到网页");
            return false;
        }
        // 把查询到的数据放入es中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("20m");
        for (int i = 0; i < news.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("southern_weekend")
                            .source(JSON.toJSONString(news.get(i)), XContentType.JSON));
        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("爬取数据成功，插入ElasticSearch成功！");
        return !bulk.hasFailures();
    }

    /**
     * 爬取百度图库图片
     *
     * @return
     * @throws Exception
     */
    public Boolean parsePictures() throws Exception {
        // 使用网页解析工具类爬取图片
        List<Pictures> pictures = new HtmlParseUtil().parsePictures();

        if (pictures.size() == 0) {
            System.out.println("未爬取到图片");
            return false;
        }
        // 把查询到的数据放入es中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("20m");
        for (int i = 0; i < pictures.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("pictures")
                            .source(JSON.toJSONString(pictures.get(i)), XContentType.JSON));
        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("爬取数据成功，插入ElasticSearch成功！");
        return !bulk.hasFailures();
    }

    public Boolean parseHotList() throws Exception {
        // 使用网页解析工具类爬取网页
        List<News> news = new HtmlParseUtil().parseNews();
        // 如果没有爬取到网页
        if (news.size() == 0) {
            System.out.println("未爬取到网页");
            return false;
        }
        // 把查询到的数据放入es中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("20m");
        for (int i = 0; i < news.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("southern_weekend")
                            .source(JSON.toJSONString(news.get(i)), XContentType.JSON));
        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("爬取数据成功，插入ElasticSearch成功！");
        return !bulk.hasFailures();
    }
}
